package com.honeycomb.action.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import com.honeycomb.action.BaseAjaxAction;
import com.honeycomb.service.IUserService;
import com.honeycomb.utils.MD5Utils;
import com.honeycomb.utils.SystemConstants;
import com.honeycomb.vo.BaseVO;
import com.honeycomb.vo.ResultDto;
import com.honeycomb.vo.UserInfoVO;
import com.honeycomb.vo.UserPasswordVO;

/**
 * @author ZhongJiang
 * 
 * @function AJAX更新用户密码
 * 
 * @param userId 用户名
 * 
 * @param passwordValue 旧口令
 * 
 * @param newPasswordValue 新口令
 * 
 * @return json
 */
public class AjaxUpdateUserPasswordAction extends BaseAjaxAction {
	
	private IUserService iUserService;
	
	public IUserService getiUserService() {
		return iUserService;
	}
	public void setiUserService(IUserService iUserService) {
		this.iUserService = iUserService;
	}

	@Override
	protected JSONObject doRequest(HttpServletRequest request,
			HttpServletResponse response, HttpSession session) throws Exception {
		logger.info("------------------AjaxUpdateUserPasswordAction-------------------");
		String userId = (String)request.getParameter("userId");
		String passwordValue = (String)request.getParameter("passwordValue");
		String newPasswordValue = (String)request.getParameter("newPasswordValue");
		
		UserPasswordVO userPasswordVO = new UserPasswordVO();
		ResultDto<BaseVO> resultDtoUpdatePassword = new ResultDto<BaseVO>();
		JSONObject jo = new JSONObject();
		try {
			userPasswordVO.setUserId(userId);
			userPasswordVO.setPasswordValue(MD5Utils.generateForPassword(passwordValue));
			userPasswordVO.setNewPasswordValue(MD5Utils.generateForPassword(newPasswordValue));
			resultDtoUpdatePassword = this.iUserService.doUpdateUserPassword(userPasswordVO);
			if(resultDtoUpdatePassword.isSuccess()) {
				jo.put("result", SystemConstants.AJAX_SUCCESS);
			}else {
				jo.put("result", SystemConstants.AJAX_FAILED_ALERT);
			}
			return jo;
		} catch (Exception e) {
			e.printStackTrace();
			jo.put("result", SystemConstants.AJAX_FAILED_ALERT);
			return jo;
		}
	}

}
